<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="账号id:">
              <el-input clearable v-model="where.id" placeholder="请输入账号id" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="主账号id:">
              <el-input clearable v-model="where.pid" placeholder="请输入主账号id" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="账号名称:">
              <el-input clearable v-model="where.username" placeholder="请输入账号名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="游戏名称">
              <PageSelect :placeholder="'请选择游戏名称'" v-model="where.game_id" :whereName="'game_name'" :url="'/game/index'"
                :optionLable="'game_name'" :optionvalue="'id'" :multiple="false" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="注册时间">
              <el-date-picker style="width: 100%" v-model="where.create_time" type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="推广员">
              <PageSelect :placeholder="'请选择推广员'" v-model="where.promote_id" :whereName="'realname'"
                :url="'/userpromote/index'" :optionLable="'realname'" :optionvalue="'id'" :multiple="false" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="手机号">
              <el-input clearable v-model="where.phone" placeholder="请输入绑定手机号" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="账号类型">
              <el-select clearable v-model="where.user_type" placeholder="请选择账号类型" style="width: 100%">
                <el-option value="0" label="全部" />
                <el-option value="1" label="主号" />
                <el-option value="2" label="小号" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="6" :md="12">
            <el-form-item label="性别:">
              <el-select
                clearable
                v-model="where.gender"
                placeholder="请选择性别"
                class="ele-fluid"
              >
                <el-option label="男" value="1" />
                <el-option label="女" value="2" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :method="'post'" :columns="columns"
        :selection.sync="selection" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <!-- <template slot="toolbar">
          <el-button
            @click="openEdit(null)"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:member:add')"
            >添加
          </el-button>
          <el-button
            @click="removeBatch"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:member:dall')"
            >删除
          </el-button>
        </template> -->
        <!-- 性别列 -->
        <!-- <template slot="gender" slot-scope="{ row }">
          <el-tag
            :type="['success', 'primary', 'warning'][row.gender - 1]"
            size="mini"
          >
            {{ ['男', '女', '保密'][row.gender - 1] }}
          </el-tag>
        </template> -->
        <!-- 头像 -->
        <!-- <template slot="avatar" slot-scope="{ row }">
          <el-avatar shape="square" :size="25" :src="row.avatar" />
        </template> -->
        <!-- 设备类型列 -->
        <!-- <template slot="device" slot-scope="{ row }">
          <el-tag
            :type="
              ['primary', 'success', 'warning', 'danger', 'info'][
                row.device - 1
              ]
            "
            size="mini"
          >
            {{ ['苹果', '安卓', 'WAP站', 'PC站', '后台'][row.device - 1] }}
          </el-tag>
        </template> -->
        <!-- 注册来源列 -->
        <template slot="source" slot-scope="{ row }">
          {{ getSourceText(row.source) }}
          <!-- 现在先不管这个 -->
          <!-- <el-tag
            :type="
              ['primary', 'success', 'warning', 'danger', 'info'][
                row.source - 1
              ]
            "
            size="mini"
          >
            {{
              ['APP注册', '小程序注册', '网站注册', 'WAP站注册', '马甲会员'][
                row.source - 1
              ]
            }}
          </el-tag> -->
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch v-model="row.status" :disabled="!permission.includes('sys:member:status')"
            @change="editStatus(row)" :active-value="1" :inactive-value="2" />
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:member:edit')">修改
          </el-link>
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openPhoneEdit(row)" v-if="
            row.pid == 0 && permission.includes('sys:member:setMemberPhone')
          ">换绑
          </el-link>
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openFillEdit(row)"
            v-if="row.pid == 0 && permission.includes('sys:member:Filledit')">补链
          </el-link>
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openTransferEdit(row)"
            v-if="row.pid != 0 && permission.includes('sys:member:Transfer')">转移
          </el-link>
          <!-- <el-popconfirm
            class="ele-action"
            title="确定要删除此会员吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:member:delete')"
              >删除
            </el-link>
          </el-popconfirm> -->
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <MemberEdit :data="current" :visible.sync="showEdit" @done="reload" />
    <MemberFillEdit :data="current" :visible.sync="showFillEdit" @done="reload" />
    <!-- 换绑弹窗 -->
    <MemberPhoneEdit :data="current" :visible.sync="showPhoneEdit" @done="reload" />

    <!-- 小号换绑 -->
    <MemberTransferEdit :data="current" :visible.sync="showTransferEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import MemberEdit from './member-edit'
import MemberFillEdit from './member-fillEdit'
import MemberTransferEdit from './member-transferEdit.vue'
import PageSelect from '@/components/pageSelect/index.vue'
import MemberPhoneEdit from './member-phoneEdit.vue'

export default {
  name: 'Member',
  components: {
    MemberEdit,
    MemberFillEdit,
    PageSelect,
    MemberTransferEdit,
    MemberPhoneEdit
  },
  computed: {
    ...mapGetters(['permission']),
    getSourceText() {
      return function (source) {
        if (source === 1) {
          return 'sdk注册'
        } else if (source === 2) {
          return '盒子'
        } else {
          return '未知'
        }
      }
    }
  },
  data() {
    return {
      // 表格数据接口
      url: '/member/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'username',
          label: '用户账号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          // formatter: (row, column, cellValue) => {
          //   let username = cellValue;
          //   if (username.length > 4) {
          //     let start = username.slice(0, username.length / 2 - 2);
          //     let end = username.slice(username.length / 2 + 2);
          //     return start + "****" + end;
          //   } else if (username.length <= 4) {
          //     username.length === 2;
          //   } else if (username.length === 2) {
          //     // 如果长度等于2，隐藏最后一位
          //     return username.charAt(0) + "*";
          //   } else if (username.length === 1) {
          //     // 如果长度等于1，隐藏全部
          //     return "*";
          //   } else {
          //     // 如果长度为0或其他情况，直接返回原值
          //     return username;
          //   }
          // }
        },
        {
          prop: 'pid',
          label: '是否为主账号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return '主账号'
            } else {
              return `小号（主账号id：${cellValue}）`
            }
          }
        },
        {
          prop: 'phone',
          label: '手机号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120
        },
        {
          prop: 'promote_account',
          label: '推广渠道',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'login_time',
          label: '最后登陆时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue)
          }
        },
        // {
        //   columnKey: 'avatar',
        //   label: '头像',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 60,
        //   slot: 'avatar'
        // },
        // {
        //   prop: 'gender',
        //   label: '性别',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 60,
        //   slot: 'gender'
        // },
        // {
        //   prop: 'device',
        //   label: '设备类型',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   slot: 'device'
        // },
        {
          prop: 'register_ip',
          label: '注册ip',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'package_name',
          label: '注册游戏',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 250
        },
        {
          prop: 'source',
          label: '注册来源',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'source'
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 80,
          resizable: false,
          slot: 'status'
        },
        {
          prop: 'create_time',
          label: '注册时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue)
          }
        },
        // {
        //   prop: 'update_time',
        //   label: '更新时间',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue)
        //   }
        // },
        {
          columnKey: 'action',
          label: '操作',
          width: 200,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: 'right'
        }
      ],
      promoteLists: [],
      // 表格搜索条件
      where: { type: '0' },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示编辑弹窗
      showFillEdit: false,
      // 小号转移至对应主账号弹框控制符
      showTransferEdit: false,
      // 手机号换绑
      showPhoneEdit: false,
      // 是否显示导入弹窗
      showImport: false
    }
  },
  created() {
    // this.getPromoteLists();
  },
  methods: {
    /* 刷新表格 */
    // getPromoteLists() {
    //   this.$http("/userpromote/index").then(res => {
    //     if (res.data.code === 0) {
    //       this.promoteLists = res.data.data;
    //     }
    //   });
    // },
    reload() {
      this.$refs.table.reload({ where: this.where })
    },
    /* 重置搜索 */
    reset() {
      this.where = { type: '0' }
      this.reload()
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row
      this.showEdit = true
    },
    openPhoneEdit(row) {
      this.current = row
      this.showPhoneEdit = true
    },
    /* 显示编辑 */
    openFillEdit(row) {
      this.current = row
      this.showFillEdit = true
    },
    //小号转移至对应主账号
    openTransferEdit(row) {
      this.current = row
      this.showTransferEdit = true
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true })
      this.$http
        .post('/member/delete', { id: row.id })
        .then((res) => {
          loading.close()
          if (res.data.code === 0) {
            this.$message.success(res.data.msg)
            this.reload()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((e) => {
          loading.close()
          this.$message.error(e.message)
        })
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return
      }
      this.$confirm('确定要删除选中的会员吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true })
          this.$http
            .post('/member/delete', { id: this.selection.map((d) => d.id) })
            .then((res) => {
              loading.close()
              if (res.data.code === 0) {
                this.$message({ type: 'success', message: res.data.msg })
                this.reload()
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              loading.close()
              this.$message.error(e.message)
            })
        })
        .catch(() => { })
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true })
      let params = Object.assign({
        id: row.id,
        status: row.status
      })
      this.$http
        .post('/member/status', params)
        .then((res) => {
          loading.close()
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: res.data.msg })
          } else {
            row.status = !row.status ? 1 : 2
            this.$message.error(res.data.msg)
          }
        })
        .catch((e) => {
          loading.close()
          this.$message.error(e.message)
        })
    }
  }
}
</script>

<style scoped></style>
